package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wit.witmg.entity.TEnergyMonitor;
import com.wit.witmg.entity.TEquip;
import com.wit.witmg.entity.TMgr;
import com.wit.witmg.mapper.TEnergyMonitorMapper;
import com.wit.witmg.service.ITEnergyMonitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.service.ITMgrService;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 能耗监测计划表 服务实现类
 * </p>
 *
 * @author bob
 * @since 2025-06-12
 */
@Service
@RequiredArgsConstructor
public class TEnergyMonitorServiceImpl extends ServiceImpl<TEnergyMonitorMapper, TEnergyMonitor> implements ITEnergyMonitorService {
    final ITMgrService mgrService;

    @Override
    public Object add(HttpServletRequest request, String json) {
        TEnergyMonitor t= JSON.parseObject(json,TEnergyMonitor.class);
        if(t.getId()>0){
            boolean bl=  updateById(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_200);
            }
        }else{
            boolean bl= save(t);
            if(!bl){
                return JsonResult.error(ErrorCode.ERR_504);
            }
        }
        return JsonResult.success(t);
    }

    @Override
    public Object get(HttpServletRequest request, String mid, String dbn, String pbn, Long dt0,Long dt1) {
        LambdaQueryWrapper<TEnergyMonitor> qw = new LambdaQueryWrapper<>();
        Optional.ofNullable(mid).ifPresent(m -> qw.eq(TEnergyMonitor::getMid, m));
        Optional.ofNullable(dbn).ifPresent(d -> qw.eq(TEnergyMonitor::getDbn, d));
        Optional.ofNullable(pbn).ifPresent(p -> qw.eq(TEnergyMonitor::getPbn, p));
//        if (dt0 != null && dt1 != null) {
//            qw.between(TEnergyMonitor::getDt0, dt0, dt1);
//            qw.between(TEnergyMonitor::getDt1, dt0, dt1);
//        }

        List<TEnergyMonitor> list = list(qw);

        if (!list.isEmpty()) {
            // 收集所有 mid
            List<String> midList = list.stream()
                    .map(TEnergyMonitor::getMid)
                    .distinct()
                    .collect(Collectors.toList());

            // 构建统一查询条件
            LambdaQueryWrapper<TMgr> qwm = new LambdaQueryWrapper<>();
            qwm.in(TMgr::getMid, midList);
            if (dt0 != null && dt1 != null) {
                qwm.between(TMgr::getDatetime, dt0, dt1);
            }

            List<TMgr> mgrList = mgrService.list(qwm);

            // 按 mid 分组
            Map<String, List<TMgr>> mgrMap = mgrList.stream()
                    .collect(Collectors.groupingBy(TMgr::getMid));

            // 绑定数据
            for (TEnergyMonitor item : list) {
                item.setMgrs(mgrMap.getOrDefault(item.getMid(), Collections.emptyList()));
            }
        }

        return JsonResult.success(list);
    }
}
